﻿using MediatR;
using Yuekao.API.Write.Command;
using Yuekao.Domain.API;
using Yuekao.Domain.BaseReposi;
using Yuekao.Domain.Entitys;

namespace Yuekao.API.Write.CommandHander
{
    public class DelDoctorHandler : IRequestHandler<DelDoctorCommand, APIResult<bool>>
    {
        private readonly IBaseRepository<Doctor> doctorRepo;

        public DelDoctorHandler(IBaseRepository<Doctor> doctorRepo)
        {
            this.doctorRepo = doctorRepo;
        }
        /// <summary>
        /// 医生管理删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<bool>> Handle(DelDoctorCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<bool> res = new APIResult<bool>();
                var doctor = await doctorRepo.GetValues(x => x.DoctorId == request.DoctorId);
                if (doctor == null)
                {
                    res.Code = APIEnum.Fail;
                    res.Message = "未找到数据";
                    res.Data = false;
                    return res;
                }
                doctor.IsDel = true;
                doctorRepo.Update(doctor);
                res.Code = APIEnum.Success;
                res.Message = "删除成功";
                res.Data = true;
                return res;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        
        }
    }
}
